package it.polimi.jita.cp.block.co;

import it.polimi.jita.cp.block.Output;
import it.polimi.jita.cp.block.pc.PenaltyInstant;
import it.polimi.jita.cp.block.pc.PenaltyOutput;

import java.util.List;

import org.apache.commons.math3.random.EmpiricalDistribution;
import org.jfree.data.category.DefaultCategoryDataset;

public class CapacityOptimizatorOutput extends Output {
	private final Double optimalSystemCapacity;
	private final PenaltyOutput penaltyOutput;

	protected CapacityOptimizatorOutput(Double optimalSystemCapacity,
			PenaltyOutput penaltyOutput){
		super();
		this.optimalSystemCapacity = optimalSystemCapacity;
		this.penaltyOutput = penaltyOutput;
	}

	public Double getOptimalSystemCapacity() {
		return optimalSystemCapacity;
	}

	public PenaltyOutput getPenaltyOutput() {
		return penaltyOutput;
	}

	// TODO ...
	public static DefaultCategoryDataset getPointsDistribution(
			CapacityOptimizatorOutput output) {

		List<PenaltyInstant> instants = output.getPenaltyOutput()
				.getPenaltyInstants();
		double[] values = new double[instants.size()];
		int i = 0;
		double max = 0;
		for (PenaltyInstant penaltyInstant : instants) {
			values[i] = penaltyInstant.getCapacityDemand();
			max = values[i] > max ? values[i] : max;
			i++;
		}
		int binCount = (int) (max / 10);
		System.out.println(binCount);
		EmpiricalDistribution ed = new EmpiricalDistribution(1);
		ed.load(values);

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for (int j = 0; j < max; j += 1) {
			dataset.addValue(
					ed.cumulativeProbability(j + 1)
							- ed.cumulativeProbability(j), "Capacity", j + "");
		}

		return dataset;

	}

}
